package com.chalk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.dao.VenueCommentDao;
import com.chalk.model.VenueComment;
import com.chalk.service.VenueCommentService;
import com.chalk.util.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-16
 */

@Service
public class VenueCommentServiceImpl extends ServiceImpl<VenueCommentDao, VenueComment> implements VenueCommentService {

    @Override
    public int addVenueComment(VenueComment entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateVenueComment(VenueComment entity) {
        return baseMapper.updateById(entity);
    }


    @Override
    public int deleteVenueComment(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public Page<VenueComment> getVenueCommentsByVenueIdForPage(Map<String, Object> map) {
        String venueId = (String) map.get("venueId");
        //排序
        List<String> desc = new ArrayList<>();
        desc.add("create_time");
        //构造器
        EntityWrapper<VenueComment> wrapper = new EntityWrapper<>();
        wrapper.eq("venue_id", venueId);
        wrapper.eq("is_delete",0);
        Page<VenueComment> page = new Page<>(MapUtil.getPage(map), MapUtil.getSize(map));
        page.setDescs(desc);
        return selectPage(page,wrapper);
    }

    @Override
    public Double getAvgScoreByVenueId(Integer venueId) {
        return this.baseMapper.getAvgScoreByVenueId(venueId);
    }

    @Override
    public Boolean publishVenueComment(VenueComment comment) {
        comment.setCreateTime(LocalDateTime.now());
        if(StringUtils.isBlank(comment.getVcImg())){
            comment.setVcType(1);
        }else {
            comment.setVcType(2);
        }
        return insert(comment);
    }
}
